package leetcode.jianzhioffer;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-19 14:17
 **/
public class JianZhiOffer05 {
    public String replaceSpace2(String s) {
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                s = s.substring(0, i) + "%20" + s.substring(i + 1);
            }
        }
        return s;
    }

    public String replaceSpace(String s) {
        char[] ch = new char[3 * s.length()];
        //用来记录字符数组当前索引位置
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                ch[count++] = '%';
                ch[count++] = '2';
                ch[count++] = '0';
                continue;
            }
            ch[count++] = s.charAt(i);
        }
        return new String(ch, 0, count);
    }

}
